<?php
/**
 *
 *
 *
 */
class Yz_Form_Element_TinyMCE
    extends Yz_Form_Element_Input
{

    const JQUERY_TINYMCE_JS = "./www/tinymce/jscripts/tiny_mce/jquery.tinymce.js";
    const TINYMCE_JS = "www/tinymce/jscripts/tiny_mce/tiny_mce.js";

    protected $name;
    protected $value = "";


    public function render(Yz_Template_Interface $template)
    {
        $text = htmlspecialchars($this->value);
        $textClick = $template->translate('Click to edit');
        $o = <<<html
            <div>
                <a href="#" onclick="return tinymceClick(this)">{$textClick}</a>
                <textarea name="{$this->name}">{$text}</textarea>
            </div>\n
html;
        $this->includeJS($template);
        return $o;
    }

    protected function includeJS(/*Yz_Template_Interface*/$t)
    {
        $webRoot = $t->webRoot;
        $tinymceJS = $webRoot . self::TINYMCE_JS;
        $initJS = $webRoot . self::JQUERY_TINYMCE_JS;
        $js = <<<js
function tinymceClick(a) {
    Yz.jquery(function(){
        Yz.fetchScript('{$initJS}', function() {
            $(a).hide()
                .next()
                .show()
                .tinymce({
                    script_url : '{$tinymceJS}',
                    dummy: false
                })
        })

    });
   return false;
}
js;
        $t->includeJS(
            'tinymce-init',
            $js
        ) ;
        return $this;
    }


}
